<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link href="/css/layui.css" rel="stylesheet">
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <input id="id" name="id" type="hidden" th:value="${product.id}">
                    <div class="input-group" id="centerIpGroup">
                        <label class="input-group-addon">商品属性：</label>
                        <div id="existingPropertiesDiv"></div>
                        <div class="input-group centerIp">
                            <label class="input-group-addon">属性名称：</label>
                            <input class="form-control rewardId" type="text" name="rewardId1" id="rewardId1"/>
                            <label class="input-group-addon">属性内容：</label>
                            <input class="form-control rewardNum" type="text" name="rewardNum1" id="rewardNum1"/>
                            <span class="input-group-btn">
			                <button class="btn btn-info" type="button" data-toggle="tooltip" title="删除"
                                    id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>
			                </span>
                        </div>
                        <button class="btn btn-info" type="button" data-toggle="tooltip" title="新增"
                                id="addCenterIpGrpBtn" onclick="addCenterIpGrp(this)"><span
                                class="glyphicon glyphicon-plus"></span></button>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-8 col-sm-offset-3">
                            <button type="submit" class="btn btn-primary" onclick="add()">提交</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script src="/js/layui.js"></script>
<script>

    var rowCount = 1;//(资源)行数默认1行

    $(function () {
        $.getJSON('/productProperty/getProperty/' + $("#id").val(), function (data) {
            if (data) {
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    rowCount++;
                    html += '<div class="input-group centerIp">' +
                        '<label class="input-group-addon">属性名称：</label>' +
                        '<input class="form-control rewardId" type="text" name="rewardId' + rowCount + '" id="rewardId' + rowCount + '" value="' + data[i].name + '">' +
                        ' <label class="input-group-addon">属性内容：</label>' +
                        ' <input class="form-control rewardNum" type="text" name="rewardNum' + rowCount + '" id="rewardNum' + rowCount + '" value="' + data[i].detail + '">' +
                        ' <span class="input-group-btn">' +
                        '<button class="btn btn-info" type="button" data-toggle="tooltip" title="删除" id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>' +
                        ' </span>' +
                        '</div>';
                }
                document.getElementById("existingPropertiesDiv").innerHTML = html;
            }
        });
    });

    $(document).on('click', '#delCenterIpGrp', function () {
        var el = this.parentNode.parentNode;
        var centerIp = $(this).parent().parent().find('#ipInput').val();
        el.parentNode.removeChild(el);
    })

    //添加资源输入框项
    function addCenterIpGrp(obj) {
        rowCount++;
        var html = '<div class="input-group centerIp">' +
            '<label class="input-group-addon">属性名称：</label>' +
            '<input class="form-control rewardId" type="text" name="rewardId' + rowCount + '" id="rewardId' + rowCount + '" >' +
            ' <label class="input-group-addon">属性内容：</label>' +
            ' <input class="form-control rewardNum" type="text" name="rewardNum' + rowCount + '" id="rewardNum' + rowCount + '" >' +
            ' <span class="input-group-btn">' +
            '<button class="btn btn-info" type="button" data-toggle="tooltip" title="删除" id="delCenterIpGrp"><span class="glyphicon glyphicon-minus"></span></button>' +
            ' </span>' +
            '</div>';
        obj.insertAdjacentHTML('beforebegin', html);
    }

    function add() {
        var rewardArray = new Array();
        for (var i = 1; i <= rowCount; i++) {
            var rewardId = $('div .input-group #rewardId' + i).val();
            var rewardNum = $('div .input-group #rewardNum' + i).val();
            if ((rewardId != '' && undefined != rewardId) || (rewardNum != '' && undefined != rewardNum)) {
                var obj = new Object();
                obj['name'] = rewardId;
                obj['detail'] = rewardNum;
                rewardArray.push(obj);
            }
        }
        var records = JSON.stringify(rewardArray);
        console.log(records);
        $.ajax({
            url: "/productProperty/add",
            type: "post",
            data: {
                'records': records,
                'productId': $("#id").val()
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }
</script>
</body>
</html>
